//
//  MyItem.m
//  Dianping
//
//  Created by 祝文博 on 15/3/4.
//  Copyright (c) 2015年 KevinLab. All rights reserved.
//

#import "DianpingStore.h"
#import "API.h"
#import "NSObject+Additions.h"

@implementation DianpingStore

- (instancetype)initWithAttributes:(NSDictionary *)attributes
{
    if (self = [super init]) {
        @try {
            self.id = [attributes[@"business_id"] toInt];
            self.name = [attributes[@"name"] toString];
            self.address = [attributes[@"address"] toString];
            self.sPhotoUrl = attributes[@"s_photo_url"];
            self.photoUrl = attributes[@"photo_url"];
        }
        @catch (NSException *exception) {
            NSLog(@"parse json to DianpingStore err");
        }
    }
    return self;
}

+ (instancetype)initWithId:(NSInteger)id name:(NSString *)name address:(NSString *)address sPhotoUrl:(NSURL *)sPhotoUrl photoUrl:(NSURL *)photoUrl
{
    DianpingStore *dianpingStore = [[self alloc] init];
    dianpingStore.photoUrl = photoUrl;
    dianpingStore.sPhotoUrl = sPhotoUrl;
    dianpingStore.name = name;
    dianpingStore.address = address;
    return dianpingStore;
}

+ (void)getStoreListWithParams:(NSDictionary *)params resultBlock:(void (^)(NSArray *, NSError *))resultBlock
{
    [[API sharedAPI] getPath:@"/v1/business/find_businesses" params:params resultBlock:^(id responseObject, NSError *error){
        NSArray *businesses = [responseObject[@"businesses"] toArray];
        NSMutableArray *storeList = [NSMutableArray array];
        for (NSDictionary *storeInfo in businesses) {
            DianpingStore *dianpingStore = [[self alloc] initWithAttributes:storeInfo];
            if (dianpingStore) {
                [storeList addObject:dianpingStore];
            }
        }
        if (resultBlock) {
            resultBlock([NSArray arrayWithArray:storeList], nil);
        }
    }];
}

@end
